﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;

using MiniSched.Orm;
using MiniSched.Web.Areas.Api.Filters;
using MiniSched.Web.Security.Abstractions;
using MiniSched.Web.Abstractions;
using MiniSched.Email;

namespace MiniSched.Web.Areas.Api.Controllers {

    [ValidateAntiForgeryToken]
    public class BaseApiController : ApiController {

        internal MembershipAbstraction MembershipAbstraction {
            get {
                if (null == _MembershipAbstraction) _MembershipAbstraction = MembershipAbstraction.Default;
                return _MembershipAbstraction;
            }
            set {
                if (null == value) throw new ArgumentNullException("MembershipAbstraction");
                _MembershipAbstraction = value;
            }
        }
        private MembershipAbstraction _MembershipAbstraction;

        internal MiniSchedContextFactory ContextFactory {
            get {
                if (null == _ContextFactory) _ContextFactory = new MiniSchedContextFactory();
                return _ContextFactory;
            }
            set {
                if (null == value) throw new ArgumentNullException("ContextFactory");
                _ContextFactory = value;
            }
        }
        private MiniSchedContextFactory _ContextFactory;

        internal MiniSchedContext Context {
            get { return _Context ?? (_Context = ContextFactory.CreateContext()); }
        }
        private MiniSchedContext _Context;

        internal WebSecurityAbstraction WebSecurity {
            get {
                if (null == _WebSecurity) _WebSecurity = WebSecurityAbstraction.Default;
                return _WebSecurity;
            }
            set {
                if (null == value) throw new ArgumentNullException("WebSecurity");
                _WebSecurity = value;
            }
        }
        private WebSecurityAbstraction _WebSecurity;

        internal MiniSchedEmailer Emailer {
            get {
                if (null == _Emailer) _Emailer = new MiniSchedEmailer();
                return _Emailer;
            }
            set {
                if (null == value) throw new ArgumentNullException("Emailer");
                _Emailer = value;
            }
        }
        private MiniSchedEmailer _Emailer;

        internal BaseApiController() { }

        internal UserProfile GetUserProfile() {
            var user = MembershipAbstraction.GetUser();
            if (user == null) return null;

            var userName = user.UserName;
            var userProfile = Context.UserProfiles.FirstOrDefault(u => u.UserName == userName);
            return userProfile;
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                if (_Context != null) {
                    _Context.Dispose();
                }
            }
            base.Dispose(disposing);
        }
    }
}
